﻿var activityWindow;

function showActivityCreateForm() {
    if (!activityWindow) {
        var fieldNo = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'No',
            allowBlank: false,
            fieldLabel: '活动编号',
            blankText: '编号不能为空',
            emptyText: '请输入编号'
        });
        var fieldTitle = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'Title',
            allowBlank: false,
            fieldLabel: '活动名称',
            blankText: '名称不能为空',
            emptyText: '请输入名称'
        });
        var fieldStartTime = new Ext.form.field.Date({
            xtype: 'datefield',
            name: 'StartTime',
            fieldLabel: '开始',
            margin: '0 5 0 0',
            allowBlank: false
        });
        var fieldEndTime = new Ext.form.field.Date({
            xtype: 'datefield',
            name: 'EndTime',
            fieldLabel: '结束',
            margin: '0 5 0 0',
            allowBlank: false
        });
        var fieldContent = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'Content',
            fieldLabel: '活动描述',
            height: 300,
            anchor: '100%'
        });

        
        var activityForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 70,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [
                    fieldNo,fieldTitle,
                    { xtype: 'fieldcontainer',
                        fieldLabel: '活动时间',
                        combineErrors: true,
                        msgTarget: 'side',
                        layout: 'hbox',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        },
                        items: [fieldStartTime, fieldEndTime]
                    }, fieldContent],

            buttons: [{
                text: '取消',
                handler: function () {
                    activityForm.getForm().reset();
                    activityWindow.close();
                }
            }, {
                text: '提交',
                handler: function () {
                    if (activityForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        activityForm.getForm().submit({
                            url: '/Activity/Create',
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { activityForm.getForm().reset(); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { activityForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        activityWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });

        activityWindow = Ext.widget('window', {
            title: '新建活动',
            closeAction: 'hide',
            width: 500,
            height: 600,
            minHeight: 300,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: activityForm
        });
    }
    activityWindow.show();
}

function showActivityEditForm(id) {
    if (!activityWindow) {
        var fieldId = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'ID',
            allowBlank: false,
            enable:false,
            fieldLabel: '活动号(不可编辑)'
        });
        var fieldNo = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'No',
            allowBlank: false,
            fieldLabel: '活动编号',
            blankText: '编号不能为空',
            emptyText: '请输入编号'
        });
        var fieldTitle = new Ext.form.field.Text({
            xtype: 'textfield',
            name: 'Title',
            allowBlank: false,
            fieldLabel: '活动名称',
            blankText: '名称不能为空',
            emptyText: '请输入名称'
        });
        var fieldStartTime = new Ext.form.field.Date({
            xtype: 'datefield',
            name: 'StartTime',
            fieldLabel: '开始',
            margin: '0 5 0 0',
            allowBlank: false
        });
        var fieldEndTime = new Ext.form.field.Date({
            xtype: 'datefield',
            name: 'EndTime',
            fieldLabel: '结束',
            margin: '0 5 0 0',
            allowBlank: false
        });
        var fieldContent = new Ext.form.field.HtmlEditor({
            xtype: 'htmleditor',
            name: 'Content',
            fieldLabel: '活动描述',
            height: 300,
            anchor: '100%'
        });


        var activityForm = Ext.widget('form', {
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            border: false,
            bodyPadding: 10,

            fieldDefaults: {
                labelAlign: 'top',
                labelWidth: 70,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0'
            },

            items: [fieldId,
                    fieldNo, fieldTitle,
                    { xtype: 'fieldcontainer',
                        fieldLabel: '活动时间',
                        combineErrors: true,
                        msgTarget: 'side',
                        layout: 'hbox',
                        defaults: {
                            flex: 1,
                            hideLabel: true
                        },
                        items: [fieldStartTime, fieldEndTime]
                    }, fieldContent],

            buttons: [{
                text: '取消',
                handler: function () {
                    activityForm.getForm().reset();
                    activityWindow.close();
                }
            }, {
                text: '提交',
                handler: function () {
                    if (activityForm.getForm().isValid()) {
                        // In a real application, this would submit the form to the configured url
                        activityForm.getForm().submit({
                            url: '/Activity/Create',
                            waitMsg: '提交中...',
                            success: function (form, action) {
                                Ext.Msg.alert('提示', '提交成功', function () { activityForm.getForm().reset(); });
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('提示', '提交失败', function () { activityForm.getForm().reset(); });
                            }
                        });
                        //registerForm.getForm().reset();
                        activityWindow.close();
                        //Ext.MessageBox.alert('Thank you!', 'Your inquiry has been sent. We will respond as soon as possible.');
                    }
                }
            }]
        });        

        activityWindow = Ext.widget('window', {
            title: '活动详细',
            closeAction: 'hide',
            width: 500,
            height: 600,
            minHeight: 300,
            layout: 'fit',
            resizable: true,
            modal: true,
            items: activityForm
        });

        var activity_data_store = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({ url: '/Activity/Details/' + id }),
            reader: new Ext.data.JsonReader(
        {
            id: 'ID'
        },
        [
        'ID', 'No', 'Title', 'Content', 'StartTime', 'EndTime'
        ]),
            remoteSort: false
        });

        activity_data_store.load();

        activity_data_store.on('load', function () {
            if (activity_data_store.getAt(0) == null) {
                Ext.Msg.alert('错误', '没有编号为' + id + '的活动信息！');
            }
            else {
                activityWindow.setTitle('编号(' + activity_data_store.getAt(0).data.No + ')活动详细信息');
                fieldId.setValue(activity_data_store.getAt(0).data.ID);
                fieldNo.setValue(activity_data_store.getAt(0).data.No);
                fieldStartTime.setValue(formatDatetime(activity_data_store.getAt(0).data.StartTime));
                fieldEndTime.setValue(formatDatetime(activity_data_store.getAt(0).data.EndTime));
                fieldTitle.setValue(activity_data_store.getAt(0).data.Title);
                fieldContent.setValue(activity_data_store.getAt(0).data.Content);
            }
        });
    }
    activityWindow.show();
}